package org.flown.shop.controller;


import jakarta.servlet.http.HttpSession;
import org.flown.shop.SessionAttribute;
import org.flown.shop.entity.Seller;
import org.flown.shop.entity.Store;
import org.flown.shop.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@RequestMapping("/seller")
@Controller
public class StoreSellerController
{
    @Autowired
    private StoreService service_store;


    @ResponseBody
    @PostMapping("/createStore")
    public String createStore(HttpSession session, @RequestParam("name") String store_name)
    {
        Seller seller = (Seller)session.getAttribute(SessionAttribute.SELLER.getName());
        if (seller == null)
            return "没登录";

        // TODO 用消息队列调用到 insertStore
        if (this.service_store.insertStore(new Store(seller.getID(), store_name)) == 0)
            return "创建失败，可能是店名已被使用";

        session.setAttribute(SessionAttribute.STORE.getName(), this.service_store.getStoreBySellerID(seller.getID()));
        return "创建成功";
    }

}

